package com.qh.book.api;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.qh.book.VO.BooklistVO;
import com.qh.book.VO.Ids;
import com.qh.book.VO.ResultVO;
import com.qh.book.converter.Book2BookDTO;
import com.qh.book.converter.Book2CarInfoDTO;
import com.qh.book.dataobj.Book;
import com.qh.book.dto.BookDTO;
import com.qh.book.dto.CarInfoDTO;
import com.qh.book.enums.BookStatusEnum;
import com.qh.book.enums.ResultEnum;
import com.qh.book.service.BookService;
import com.qh.book.utils.ResultVOUtil;
import com.qh.book.utils.StrUtils;

@CrossOrigin
@RestController
@RequestMapping("/api/book")
public class BookApi {
	
	@Autowired
	private BookService bookService;
	
	/**
	 * 
	 * TODO 返回书籍列表
	 * @param page
	 * @param key
	 * @return
	 * @author qihong
	 */
	@GetMapping("/booklist")
	public ResultVO<BooklistVO> booklist(@RequestParam(value = "page", defaultValue = "1") Integer page,
						@RequestParam(value = "key",defaultValue = "") String key) {
		
		Book book = new Book();
		book.setBookName(key);
		book.setBookStatus(BookStatusEnum.UP.getCode());
		Pageable pageable = new PageRequest(page-1, 10);
		Page<Book> pageBook = bookService.findAll(book, pageable);
		List<BookDTO> bookDtoList=  Book2BookDTO.converter(pageBook.getContent());
		BooklistVO<BookDTO> booklistVO = new BooklistVO<BookDTO>();
		booklistVO.setTotal(pageBook.getTotalElements());
		booklistVO.setList(bookDtoList);
		ResultVO<BooklistVO> resultVO = new ResultVO<BooklistVO>();
		resultVO.setStatus(ResultEnum.SCUUESS.getCode());
		resultVO.setMsg(ResultEnum.SCUUESS.getMsg());
		resultVO.setData(booklistVO);
		return resultVO;
		
	}
	
	/**
	 * 
	 * TODO 返回书籍详细信息
	 * @param id
	 * @return
	 * @author qihong
	 */
	@GetMapping("/bookinfo")
	public ResultVO<Object> bookinfo(@RequestParam(value = "id", defaultValue = "") String id) {
		//对传递的参数进行检查
		String bookid = id;
		ResultVO<Object> resultVO = new ResultVO<Object>();
		if("".equals(bookid)||!StrUtils.isInteger(bookid)) {
			ResultVOUtil.returnError(ResultEnum.PARM_FAIL);
			return resultVO;
		}
		//查询
		Book book = bookService.findOne(bookid);
		if(book==null) {
			ResultVOUtil.returnError(ResultEnum.BOOK_NOT_EXIST);
			return resultVO;
		}
	
		//封装返回数据
		BookDTO dto = Book2BookDTO.converter(book);

		resultVO = ResultVOUtil.returnSuccess(ResultEnum.SCUUESS, dto);
		return resultVO;
	
		
	}
	/**
	 * 
	 * TODO 根据id数组返回商品
	 * 注意这里不是返回购物车 
	 * @param ids
	 * @return
	 * @author qihong
	 * 暂时用get吧  post方式硬是没有把这个数组传上来
	 */
	@GetMapping("/getcarinfo")
	public ResultVO<Object>  getCarInfo(@RequestParam(value="ids" ,required = false) String ids ) {
		
		//System.out.println(ids);
		String[] idStr = ids.split("_");
		ResultVO<Object> resultVO = new ResultVO<Object>(); 
		List<String> idlist = new ArrayList<String>();
		for (String id : idStr) {
			if(id!=null) {
				idlist.add(id);
			}
		}
		//idlist.toArray(idStr);
		List<Book> bookList = bookService.findByBookIdIn(idlist);
		List<CarInfoDTO> carInfoDTOList = Book2CarInfoDTO.converter(bookList);
		BooklistVO<CarInfoDTO> booklistVO = new BooklistVO<CarInfoDTO>();
		booklistVO.setTotal(bookList.size());
		booklistVO.setList(carInfoDTOList);
		resultVO = ResultVOUtil.returnSuccess(ResultEnum.SCUUESS, booklistVO);
		return resultVO;
	}
	

}
